
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);

//背景图片
var bgReady = false;
var bgImage = new Image();
bgImage.onload = function () {
	bgReady = true;
};
bgImage.src = "images/background.png";

//英雄图片
var heroReady = false;
var heroImage = new Image();
heroImage.onload = function () {
	heroReady = true;
};
heroImage.src = "images/hero.png";

//怪物图片
var monsterReady = false;
var monsterImage = new Image();
monsterImage.onload = function () {
	monsterReady = true;
};
monsterImage.src = "images/monster.png";
//蘑菇图片
var mushroomReady=false
var mushroomImage=new Image();
mushroomImage.onload=function(){
	mushroomReady=true;
}
mushroomImage.src="images/蘑菇.png"
//英雄
var hero = {
	speed: 256 
};
var monster = {};
var monstersCaught = 0;

var mushroom={}

// 键盘
var keysDown = {};

addEventListener("keydown", function (e) {
	keysDown[e.keyCode] = true;
}, false);

addEventListener("keyup", function (e) {
	delete keysDown[e.keyCode];
}, false);

// 重置
var reset = function () {
	hero.x = canvas.width / 2;
	hero.y = canvas.height / 2;

	
	monster.x = 32 + (Math.random() * (canvas.width - 108));
	monster.y = 32 + (Math.random() * (canvas.height - 108));
	
	mushroom.x=32 + (Math.random() * (canvas.width - 108));
	mushroom.y=32 + (Math.random() * (canvas.width - 108));
};


// 更新
var update = function (modifier) {
	if (38 in keysDown && hero.y>30) { // Player holding up
		hero.y -= hero.speed * modifier;
	}
	if (40 in keysDown &&hero.y<416) { // Player holding down
		hero.y += hero.speed * modifier;
	}
	if (37 in keysDown &&hero.x>30) { // Player holding left
		hero.x -= hero.speed * modifier;
	}
	if (39 in keysDown &&hero.x<450) { // Player holding right
		hero.x += hero.speed * modifier;
	}

	// 捕捉判定
	if (
		hero.x <= (monster.x + 32)
		&& monster.x <= (hero.x + 32)
		&& hero.y <= (monster.y + 32)
		&& monster.y <= (hero.y + 32)
	) {
		++monstersCaught;
		reset();
		if(monstersCaught%2==0){
		monsterImage.src="images/monster.png"
		heroImage.src="images/hero.png"
		}
		else{
		monsterImage.src="images/hero.png"
		heroImage.src="images/monster.png"
		}
	}
//	if(hero.x <= (mushroom.x + 32)
//		&& mushroom.x <= (hero.x + 32)
//		&& hero.y <= (monster.y + 32)
//		&& mushroom.y <= (hero.y + 32))
//	{
//		speed=speed+10
//	}
};


var render = function () {
	if (bgReady) {
		ctx.drawImage(bgImage, 0, 0);
	}

	if (heroReady) {
		ctx.drawImage(heroImage, hero.x, hero.y);
	}

	if (monsterReady) {
		ctx.drawImage(monsterImage, monster.x, monster.y);
	}

	//得分
	ctx.fillStyle = "rgb(250, 250, 250)";
	ctx.font = "24px Helvetica";
	ctx.textAlign = "left";
	ctx.textBaseline = "top";
	ctx.fillText("地精被捕数量: " + monstersCaught, 32, 32);
};


var main = function () {
	var now = Date.now();
	var delta = now - then;

	update(delta / 1000);
	render();

	then = now;

	// Request to do this again ASAP
	requestAnimationFrame(main);
};

// Cross-browser support for requestAnimationFrame
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;

// Let's play this game!
var then = Date.now();
reset();
main();
